package com.mjh.zhujianju.controller.party;

import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.utils.XTCurrentUser;
import com.mjh.zhujianju.model.base.UserExtendEO;
import com.mjh.zhujianju.model.party.BuildingClassEO;
import com.mjh.zhujianju.service.base.UserExtendService;
import com.mjh.zhujianju.service.party.BuildingClassDetailService;
import com.mjh.zhujianju.service.party.BuildingClassService;
import com.mjh.zhujianju.service.party.BuildingClassUsersService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import com.mjh.zhujianju.vo.party.BuildingClassVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author liuyang
 * @date 2018/9/5 21:53
 * @Description: 课堂学习
 */
@RestController
@RequestMapping("/class")
@Api("课堂学习")
public class ClassController {

    @Autowired
    private UserExtendService userExtendService;

    @Autowired
    private BuildingClassUsersService buildingClassUsersService;

    @Autowired
    private BuildingClassDetailService buildingClassDetailService;

    @Autowired
    private BuildingClassService buildingClassService;

    @Resource
    private SysUserService userService;

    @ApiOperation(value="加入学习计划", notes="加入学习计划，只能党员能参加")
    @RequestMapping(value = "/addPlay/{classId}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课题id", required = true, dataType = "Long", paramType = "path")
    })
    public ResponseEntity<JsonResult> add (@PathVariable(value = "classId") Long classId){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                //党员验证
                UserExtendEO userExtendEO = userExtendService.queryUserExtendByUserId(userId);
                if (userExtendEO==null || !userExtendEO.getIsParty().equals("1")){
                    result.setResult("不是党员");
                    result.setStatus(ApiConstant.MSG_NO_PARTY);
                    return ResponseEntity.ok(result);
                }
                //保存
                buildingClassUsersService.addJoinClass(userId,classId);
                result.setStatus(ApiConstant.MSG_SUCCESS);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="查看课件详情", notes="课件查看")
    @RequestMapping(value = "/joinClass/{classDetailId}/", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classDetailId", value = "课件id", required = true, dataType = "Long", paramType = "path")
    })
    public ResponseEntity<JsonResult> detail (@PathVariable(value = "classDetailId") Long classDetailId){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                //文件id
                Long fileId =  buildingClassDetailService.queryClassDetailFileId(classDetailId,userId);
                result.setStatus(ApiConstant.MSG_SUCCESS);
                result.setResult(fileId);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }


    @ApiOperation(value="取消报名", notes="用户取消参加的课程")
    @RequestMapping(value = "/cancel/{classId}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "课件id", required = true, dataType = "Long", paramType = "path")
    })
    public ResponseEntity<JsonResult> cancel (@PathVariable(value = "classId") Long classId){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                //取消报名
                buildingClassUsersService.updateClassUser(classId,userId);
                result.setStatus(ApiConstant.MSG_SUCCESS);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="恢复报名", notes="用户恢复参加的课程")
    @RequestMapping(value = "/restore/{classId}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "课件id", required = true, dataType = "Long", paramType = "path")
    })
    public ResponseEntity<JsonResult> restore (@PathVariable(value = "classId") Long classId){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                //恢复报名
                buildingClassUsersService.updateClassUserRestore(classId,userId);
                result.setStatus(ApiConstant.MSG_SUCCESS);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="学习计划", notes="查询用户的学习计划 finish：完成，unfinished：未完成")
    @RequestMapping(value = "/playList", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> playList (){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                Map<String,List<BuildingClassVO>> map = buildingClassService.queryPlayList(userId);
                result.setStatus(ApiConstant.MSG_SUCCESS);
                result.setResult(map);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="学习计划->详情", notes="根据微课堂id查询详细信息")
    @RequestMapping(value = "/play/detail/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "微课堂id", required = true, dataType = "Long", paramType = "path")
    })
    public ResponseEntity<JsonResult> getRemoveInfo (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                BuildingClassVO buildingClassVO = buildingClassService.queryBuildingClassInfoPlay(userId,id);
                result.setResult(buildingClassVO);
                result.setStatus(ApiConstant.MSG_SUCCESS);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }



}
